package com.rljk.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Author:   YASUO
 * Date:     2018/8/13 18:52
 * Description:
 */
public class PictureUtils {

    //  根据java 代码生成图片保存成文件
    private static void createImage(File file) throws IOException {
        Font font = new Font("微软雅黑", Font.BOLD, 25);
        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
//        Graphics2D g2 = (Graphics2D) bi.getGraphics();
//        g2.setBackground(Color.WHITE);
//        g2.clearRect(0, 0, 200, 200);
//        g2.setPaint(Color.BLACK);
//        g2.setFont(font);
//        FontRenderContext context = g2.getFontRenderContext();
//        Rectangle2D bounds = font.getStringBounds(s, context);
//        double x = (200 - bounds.getWidth()) / 2;
//        double y = (200 - bounds.getHeight()) / 2;
//        double ascent = -bounds.getY();
//        double baseY = y + ascent;
//        g2.drawString(s, (int) x, (int) baseY);
        ImageIO.write(bi, "jpg", file);
    }


    public static void main(String[] args) throws  Exception{
        File file = new File("C:/D/AAA.JPG");
        String s = "";
        createImage(file);
        System.out.println("ok");
    }

}
